package com.lv.phuotbacnam.bl.queries;

import java.util.List;

import com.lv.phuotbacnam.bl.callback.CallBackFromParse;
import com.lv.phuotbacnam.bl.data.BaiDangObject;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;

public class BaiDangQueries {

	public static void createNewBaiDang(BaiDangObject baiDangObject,
			ParseObject diadiem) {
		ParseObject baidang = new ParseObject(
				QueriesConstant.TableBaiDang.Table_Name);
		baidang.put(QueriesConstant.TableBaiDang.COLUMN_cam_nhan,
				baiDangObject.getCam_nhan());
		// TODO dang test nen se comment code dong ben duoi
		// baidang.put(QueriesConstant.TableBaiDang.COLUMN_dia_diem, diadiem);
		baidang.put(QueriesConstant.TableBaiDang.COLUMN_nguoi_dang,
				ParseUser.getCurrentUser());
		baidang.put(QueriesConstant.TableBaiDang.COLUMN_list_photo,
				baiDangObject.getList_photo());
		baidang.put(QueriesConstant.TableBaiDang.COLUMN_loi_khuyen,
				baiDangObject.getLoi_khuyen());
		baidang.put(QueriesConstant.TableBaiDang.COLUMN_ten_bai_dang,
				baiDangObject.getTen_bai_dang());
		baidang.saveInBackground();
	}

	/**
	 * Lay list bai dang moi nhat
	 */
	public static void getBaiDangNewest(
			final CallBackFromParse<Object, List<ParseObject>> callBack) {
		ParseQuery<ParseObject> query = ParseQuery
				.getQuery(QueriesConstant.TableBaiDang.Table_Name);
		query.include(QueriesConstant.TableBaiDang.COLUMN_nguoi_dang);
		query.include(QueriesConstant.TableBaiDang.COLUMN_dia_diem);
		query.orderByDescending(QueriesConstant.TableBaiDang.COLUMN_createdAt);
		query.setLimit(QueriesConstant.LIMIT);
		query.findInBackground(new FindCallback<ParseObject>() {
			@Override
			public void done(List<ParseObject> diadiem, ParseException e) {
				if (e == null)
					callBack.done("", diadiem);
			}
		});
	}

	/**
	 * To update bai dang
	 * 
	 * @param baidang
	 * @param baiDangObject
	 * @param callBack
	 */
	public static void updateBaiDang(ParseObject baidang,
			BaiDangObject baiDangObject,
			CallBackFromParse<Object, ParseObject> callBack) {
		baidang.put(QueriesConstant.TableBaiDang.COLUMN_cam_nhan,
				baiDangObject.getCam_nhan());
		// TODO khong cho sua dia diem cua bai dang
		// baidang.put(QueriesConstant.TableBaiDang.COLUMN_dia_diem, diadiem);
		baidang.put(QueriesConstant.TableBaiDang.COLUMN_nguoi_dang,
				ParseUser.getCurrentUser());
		baidang.put(QueriesConstant.TableBaiDang.COLUMN_list_photo,
				baiDangObject.getList_photo());
		baidang.put(QueriesConstant.TableBaiDang.COLUMN_loi_khuyen,
				baiDangObject.getLoi_khuyen());
		baidang.put(QueriesConstant.TableBaiDang.COLUMN_ten_bai_dang,
				baiDangObject.getTen_bai_dang());
		baidang.saveInBackground();
	}

	/**
	 * To get list of baidang by a diadiem
	 * 
	 * @param diadiem
	 * @param callBack
	 */
	public static void getBaiDangByDiaDiem(ParseObject diadiem,
			final CallBackFromParse<Object, List<ParseObject>> callBack) {
		ParseQuery<ParseObject> query = ParseQuery
				.getQuery(QueriesConstant.TableBaiDang.Table_Name);
		query.whereEqualTo(QueriesConstant.TableBaiDang.COLUMN_dia_diem,
				diadiem);
		query.include(QueriesConstant.TableBaiDang.COLUMN_nguoi_dang);
		query.include(QueriesConstant.TableBaiDang.COLUMN_dia_diem);
		query.orderByDescending(QueriesConstant.TableBaiDang.COLUMN_luot_thich);
		query.setLimit(QueriesConstant.LIMIT);
		query.findInBackground(new FindCallback<ParseObject>() {
			@Override
			public void done(List<ParseObject> baidangs, ParseException e) {
				if (e == null && baidangs.size() > 0) {
					callBack.done("", baidangs);
				} else
					callBack.done("", null);
			}
		});
	}

	public static void updateLuotThich(ParseObject baidang) {
		int luot_thich = baidang
				.getInt(QueriesConstant.TableBaiDang.COLUMN_luot_thich);
		baidang.put(QueriesConstant.TableBaiDang.COLUMN_luot_thich,
				luot_thich + 1);
		baidang.saveInBackground();

	}

	public static void updateLuotBinhLuan(ParseObject baidang) {
		int luot_binh_luan = baidang
				.getInt(QueriesConstant.TableBaiDang.COLUMN_luot_nhan_xet);
		baidang.put(QueriesConstant.TableBaiDang.COLUMN_luot_nhan_xet,
				luot_binh_luan + 1);
		baidang.saveInBackground();
	}
}
